#include "stdafx.h"
#include "BlendState.h"

namespace Reign
{namespace Video
{namespace D3D10
{
	#pragma region Properties
	D3D10_BLEND_DESC* BlendStateDesc::Desc::get() {return desc;}
	#pragma endregion

	#pragma region Constructors
	BlendStateDesc::BlendStateDesc(BlendStateTypes type)
	{
		null();
		desc = new D3D10_BLEND_DESC();
		ZeroMemory(desc, sizeof(D3D10_BLEND_DESC));

		desc->AlphaToCoverageEnable = false;

		for (int i = 0; i != 8; ++i)
		{
			desc->RenderTargetWriteMask[i] = D3D10_COLOR_WRITE_ENABLE_ALL;
			desc->BlendEnable[i] = false;
		}
		
		switch (type)
		{
			case (BlendStateTypes::None):
				desc->BlendOp = D3D10_BLEND_OP_ADD;
				desc->SrcBlend = D3D10_BLEND_ONE;
				desc->DestBlend = D3D10_BLEND_ZERO;

				desc->BlendOpAlpha = D3D10_BLEND_OP_ADD;
				desc->SrcBlendAlpha = D3D10_BLEND_ONE;
				desc->DestBlendAlpha = D3D10_BLEND_ONE;
				break;

			case (BlendStateTypes::Add):
				for (int i = 0; i != 8; ++i) desc->BlendEnable[i] = true;
				desc->BlendOp = D3D10_BLEND_OP_ADD;
				desc->SrcBlend = D3D10_BLEND_ONE;
				desc->DestBlend = D3D10_BLEND_ONE;

				desc->BlendOpAlpha = D3D10_BLEND_OP_ADD;
				desc->SrcBlendAlpha = D3D10_BLEND_ONE;
				desc->DestBlendAlpha = D3D10_BLEND_ONE;
				break;

			case (BlendStateTypes::Subtract):
				for (int i = 0; i != 8; ++i) desc->BlendEnable[i] = true;
				desc->BlendOp = D3D10_BLEND_OP_SUBTRACT;
				desc->SrcBlend = D3D10_BLEND_ONE;
				desc->DestBlend = D3D10_BLEND_ONE;

				desc->BlendOpAlpha = D3D10_BLEND_OP_SUBTRACT;
				desc->SrcBlendAlpha = D3D10_BLEND_ONE;
				desc->DestBlendAlpha = D3D10_BLEND_ONE;
				break;

			case (BlendStateTypes::RevSubtract):
				for (int i = 0; i != 8; ++i) desc->BlendEnable[i] = true;
				desc->BlendOp = D3D10_BLEND_OP_REV_SUBTRACT;
				desc->SrcBlend = D3D10_BLEND_ONE;
				desc->DestBlend = D3D10_BLEND_ONE;

				desc->BlendOpAlpha = D3D10_BLEND_OP_REV_SUBTRACT;
				desc->SrcBlendAlpha = D3D10_BLEND_ONE;
				desc->DestBlendAlpha = D3D10_BLEND_ONE;
				break;

			case (BlendStateTypes::Alpha):
				for (int i = 0; i != 8; ++i) desc->BlendEnable[i] = true;
				desc->BlendOp = D3D10_BLEND_OP_ADD;
				desc->SrcBlend = D3D10_BLEND_SRC_ALPHA;
				desc->DestBlend = D3D10_BLEND_INV_SRC_ALPHA;

				desc->BlendOpAlpha = D3D10_BLEND_OP_ADD;
				desc->SrcBlendAlpha = D3D10_BLEND_SRC_ALPHA;
				desc->DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
				break;

			default:
				Debug::ThrowError(L"BlendStateDesc", L"Unsuported BlendStateType");
				break;
		}
	}

	BlendStateDesc::~BlendStateDesc()
	{
		if (desc) delete desc;
		null();
	}

	void BlendStateDesc::null()
	{
		desc = 0;
	}
	#pragma endregion
}
}
}